﻿using MetroDataApi.Controllers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MetroDataApi.Models
{
    public class RecentChangesListHelper
    {
        public static string GetChangesByList(int prjId, string type, string location, string timeList, bool isDesign = false)
        {
            using (var db = new MetroDataDataContext())
            {
                var names = db.MonitorPointsDictionary.Where(p => p.PrjId == prjId && p.MonitorType == type && p.Location == location).Select(p => p.Name).ToList();
                //按照时间顺序排序
                var timeListA = timeList.Split(',').ToList();

                var dates = (from t in timeListA select Convert.ToDateTime(t)).OrderBy(t => t).ToList();
                return RecentChangesListHelper.GetChanges(prjId, names, dates, type, location, isDesign);

            }
        }

        public static string GetChanges(int prjId, List<string> names, List<DateTime> dates, string type, string location, bool isDesign)
        {
            using (var db = new MetroDataDataContext())
            {

                var originValues = (isDesign == false) ? QueryResults.GetOriginResults(prjId, names) : null;
                var changes = new List<IEnumerable<result>>();

                foreach (var date in dates)
                {
                    var change = QueryResults.GetResultsByTime(prjId, date.ToString("yyyy-MM-dd HH:mm:ss"), names);
                    if (isDesign == false)
                    {
                        var result = QueryResults.GetDiffBy2ResultsList(originValues, change);
                        changes.Add(result);
                    }
                    else
                    {
                        var result = change.Select(p =>
                            new result() { name = p.name, time = p.time, value = Math.Round((p.value - 5.5) * 1000, 2) });
                        changes.Add(result);
                    }

                }



                var max = changes.Count > 0 ? changes.Max(p => p.Where(t => !double.IsNaN(t.value)).Max(t => t.value)) : 0;
                var min = changes.Count > 0 ? changes.Min(p => p.Where(t => !double.IsNaN(t.value)).Min(t => t.value)) : 0;

                return JsonConvert.SerializeObject(new
                {
                    dataList = changes,
                    max = max,
                    min = min,
                    title = isDesign == false ? string.Format("{0}-{1}", location, type) : string.Format("{0}-{1}-设计值", location, type)
                },
                Formatting.None,
                new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore,
                    Converters = new List<JsonConverter> { new StandardFloatConverter() }
                });
            }
        }

        public static string GetChangesByStartAndEnd(int prjId, string type, string location, string timeList, bool isDesign = false)
        {
            using (var db = new MetroDataDataContext())
            {
                //按照时间顺序排序
                var names = db.MonitorPointsDictionary.Where(p => p.PrjId == prjId && p.MonitorType == type && p.Location == location).Select(p => p.Name).ToList();
                var timeListA = timeList.Split(',').ToList();
                var dates = (from t in timeListA select Convert.ToDateTime(t)).OrderBy(t => t).ToList();
                var times = (from t in db.MonitorPoints
                             where t.PrjId == prjId && t.Name == names[0] && t.DateTime.Value >= dates[0] && t.DateTime.Value <= dates[1]
                             orderby t.DateTime descending
                             select t.DateTime.Value).ToList();
                var resultTimes = new List<DateTime>();
                if (times.Count > 4)
                {
                    resultTimes.Add(times.Last());
                    resultTimes.AddRange(times.Take(3).Reverse());
                }
                else
                {
                    resultTimes.AddRange(times.Take(4).Reverse());
                }
                times.Reverse();

                return RecentChangesListHelper.GetChanges(prjId, names, resultTimes, type, location, isDesign);
            }

        }
    }
}